package com.spring.oaassist.web.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.spring.oaassist.web.dao.BudgetMapper;
import com.spring.oaassist.web.model.BudgetColums;
import com.spring.oaassist.web.model.BudgetData;
import com.spring.oaassist.web.service.BudgetService;
import com.spring.security.core.util.ApplicationUtils;

@Service
public class BudgetServiceImpl implements BudgetService{
	
	@Resource private BudgetMapper budgetMapper;

	@Override
	public Map<String, Object> getBudgetHeadList(String budgetId) {
		Map<String, Object> retM = Maps.newHashMap();
		Integer icount = budgetMapper.getBudgetItemCount(budgetId);
		String field = "";
		if(icount>0){
			String itemStr = "model: { fields: { itemName: {editable: false} } }";
			retM.put("hasitem", itemStr);
			field += "{ field:\"itemName\", title: \"项目\",width: \"150px\"},";
		}
		List<BudgetColums> el = budgetMapper.getBudgetHeadList(budgetId);
		
		if(el!=null&&el.size()>0){
			for(BudgetColums cl:el){
				field += "{ field:\""+cl.getField()+"\", title: \""+cl.getTitle()+"\",width: \""+cl.getWidth()+"px\"},";
			}
			retM.put("element", field.substring(0,field.length()-1));
		}
		return retM;
	}

	/**
	 * 根据报表项目及元素查询数据
	 */
	@Override
	public List<Map<String, Object>> queryBudgetData(Map<String, Object> parm) {
		String reportID = ApplicationUtils.Object2String(parm.get("reportID"));
		String reportDate = ApplicationUtils.Object2String(parm.get("reportDate"));
		if(StringUtils.isEmpty(reportID)||StringUtils.isEmpty(reportDate)){
			return null;
		}
		
		List<BudgetData> rd = budgetMapper.queryBudgetData(parm);
		List<Map<String,Object>> retL = getDataByList(rd);
		return retL;
	}
	
	private  List<Map<String,Object>> getDataByList(List<BudgetData> dl){
		List<Map<String,Object>> retL = Lists.newArrayList();
		Map<String,Object> retM = Maps.newHashMap();
		int i = 0;//循环下标
		int bfindex = 0;//当前项目所在行
		if(dl!=null&&dl.size()>0){
			for(BudgetData bd:dl){
				if(i!=0){//第一次无需判断
					if(bfindex!=bd.getItem_index()){//行数不相等，需换行
						retL.add(retM);
						retM = Maps.newHashMap();
						if(ApplicationUtils.isNotNull(bd.getItem())){
							retM.put("item", bd.getItem());
							retM.put("itemName", bd.getItem_name());
							retM.put("itemIndex", bd.getItem_index());
						}
					}
				}else{
					if(ApplicationUtils.isNotNull(bd.getItem())){
						retM.put("item", bd.getItem());
						retM.put("itemName", bd.getItem_name());
						retM.put("itemIndex", bd.getItem_index());
					}
				}
				if(ApplicationUtils.isNotNull(bd.getElement())){
					retM.put(bd.getElement(), bd.getValue());
				}
				bfindex = bd.getItem_index();
				i++;
				if(i==dl.size()){//循环结束
					retL.add(retM);
				}
			}
		}
		
		return retL;
	}

	@Override
	public Map<String, Object> saveBudgetReport(List<Map<String, Object>> subjson) {
		Map<String, Object> parm = Maps.newHashMap();
		Map<String, Object> retM = Maps.newHashMap();
		if(subjson!=null&&subjson.size()>0){
			parm = subjson.get(0);
			subjson.remove(0);
		}
		if(StringUtils.isEmpty(ApplicationUtils.Object2String(parm.get("reportID")))||
				StringUtils.isEmpty(ApplicationUtils.Object2String(parm.get("reportDate")))){
			return null;
		}
//		String reportUnit = ApplicationUtils.Object2String(parm.get("reportUnit"));
		String reportDate = ApplicationUtils.Object2String(parm.get("reportDate"));
//		Date rdate = null;
//		if("nd".equalsIgnoreCase(reportUnit)){
//			rdate = UDate.stringToDate(reportDate, "yyyy");
//		}else if("yd".equalsIgnoreCase(reportUnit)){
//			rdate = UDate.stringToDate(reportDate, "yyyy-MM");
//		}else{
//			retM.put(ApplicationUtils.STATUS, "0");
//			retM.put(ApplicationUtils.MSG, "日期单位有误");
//			return retM;
//		}
		//先将删除
		budgetMapper.deleteBudgetReport(parm);
		for(Map<String, Object> map:subjson){
			map.put("reportID", parm.get("reportID"));
			map.put("reportDate", reportDate);
			
			Map<String, Object> insertMap = Maps.newHashMap();
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				insertMap.put(entry.getKey(), entry.getValue());
			}
			for (Map.Entry<String, Object> entry : map.entrySet()) { 
				if(!"itemIndex".equalsIgnoreCase(entry.getKey())&&!"itemName".equalsIgnoreCase(entry.getKey())&&
						!"item".equalsIgnoreCase(entry.getKey())&&!"reportID".equalsIgnoreCase(entry.getKey())&&
						!"reportDate".equalsIgnoreCase(entry.getKey())){
					insertMap.put("element", entry.getKey());
					insertMap.put("value", entry.getValue());
					budgetMapper.saveBudgetReport(insertMap);
				}
			} 
		}
		retM.put(ApplicationUtils.STATUS, "1");
		retM.put(ApplicationUtils.MSG, "操作成功");
		return retM;
	}

}
